package com.bingkaimedia.dompetobeta.app;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.bingkaimedia.dompetobeta.R;
import com.bingkaimedia.dompetobeta.libs.JSONfunctions;
import com.bingkaimedia.dompetobeta.libs.LazyAdapter2;
import com.bingkaimedia.dompetobeta.libs.SessionManager;
import com.bingkaimedia.dompetobeta.libs_image.ImageLoader;
import com.bingkaimedia.dompetobeta.libs_slidemenu.BaseActivity;

import android.annotation.SuppressLint;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

@SuppressLint("NewApi")
public class Profil  extends BaseActivity {
	
	int current_page = 0;
	
	
	
	// XML node keys 
	//static final String KEY_SONG = "song"; // parent node
	public static final String KEY_VOUCHER_ID = "voucher_id";
	public static final String KEY_VOUCHER_NAME = "voucher_name"; 
	public static final String KEY_VOUCHER_TOTAL = "voucher_total";
	public static final String KEY_VOUCHER_TERSEDIA = "voucher_tersedia";
	public static final String KEY_VOUCHER_DETAIL = "voucher_detail";
	public static final String KEY_VOUCHER_DATE = "voucher_date";
	public static final String KEY_VOUCHER_DESCRIPTION = "voucher_description";
	public static final String KEY_MERCHANT_ID = "merchant_id";
	public static final String KEY_MERCHANT_NAME = "merchant_name";
	public static final String KEY_MERCHANT_CATEGORY = "merchant_category";
	public static final String KEY_MERCHANT_PHOTO = "merchant_photo";
	public static final String KEY_TRANSACTION_ID = "transaction_id";
	public static final String KEY_TRANSACTION_DATE = "transaction_date";
	public static final String KEY_TRANSACTION_IMAGE = "transaction_image";
	public static final String KEY_TRANSACTION_STATUS = "transaction_status";
	
	public static final String KEY_MY_CHANNEL_ID = "my_channel_id";
	public static final String KEY_CHANNEL_ID = "channel_id"; 
	public static final String KEY_CHANNEL_NAME = "channel_name";
	public static final String KEY_CHANNEL_ADDRESS = "channel_address";
	public static final String KEY_CHANNEL_PHOTO = "channel_photo";
	public static final String KEY_CHANNEL_NOTE = "channel_note";
	 
	
	public static final String KEY_USERS_ID = "users_id";
	public static final String KEY_USERS_USERNAME = "users_username";
	public static final String KEY_USERS_EMAIL = "users_email";
	public static final String KEY_USERS_CITY = "users_city";
	public static final String KEY_USERS_PHOTO = "users_photo";
	public static final String KEY_USERS_PHONE = "users_phone";
	public static final String KEY_USERS_AREA = "users_area";
	
	private String xml_voucher_id;
	private String xml_voucher_name;
	private String xml_voucher_total;
	private String xml_voucher_tersedia;
	private String xml_voucher_description;
	private String xml_voucher_detail;
	private String xml_voucher_date;
	private String xml_merchant_id;
	private String xml_merchant_name;
	private String xml_merchant_category;
	private String xml_merchant_photo;
	private String xml_transaction_id;
	private String xml_transaction_date;
	private String xml_transaction_image;
	private String xml_transaction_status;
	

	private String xml_my_channel_id;
	private String xml_channel_id;
	private String xml_channel_name;
	private String xml_channel_photo;
	private String xml_channel_note;
	private String xml_channel_address;
	

	private int count;
	private String json; 
	private JSONObject jsonobject;
	public String code;
	public String msg;
	
	private String xml_users_id;
	private String xml_users_username;
	private String xml_users_email;
	private String xml_users_city;
	private String xml_users_photo;
	private String xml_users_phone;
	private String xml_users_area;
	
	private String session_userid;
	private String session_username;
	private String session_password;
	private String session_email;
	private String session_city;
	private String session_checked;
    
    
    
    public ImageLoader imageLoader; 
    
    private String idtransaksi;
    
 // Session Manager Class
 	SessionManager session;
 	
 	LazyAdapter2 adapter;
 	ArrayList<HashMap<String, String>> menuItems;
 	ProgressDialog pDialog;
 	HashMap<String, String> map = new HashMap<String, String>();
 	
 	ArrayList<HashMap<String, String>> diskonba_list = new ArrayList<HashMap<String, String>>();
 	
 	//ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
 	
 	//slide menu
 	private LinearLayout slidingPanel;
 	private boolean isExpanded;
 	private DisplayMetrics metrics;
 	private RelativeLayout headerPanel;
 	private int panelWidth;
 	private int panelWidth1;

 	private String sta="";
 	private String id_;
 	ArrayList nameValuePairs;
	String result = null;
	InputStream is = null;
	StringBuilder sb=null;
	ImageLoader imgLoader;
	String tanggal1,tanggal2;
 	private ImageView menuViewButton,menuRightButton;

 	FrameLayout.LayoutParams menuPanelParameters;
 	FrameLayout.LayoutParams slidingPanelParameters;
 	LinearLayout.LayoutParams headerPanelParameters;
 	LinearLayout.LayoutParams listViewParameters;
 	

	private Button send;
	private Button cancel;
	
	private SlidingMenu sm;
	 
	public Profil() {
		super(R.string.baru); 
	}


	@Override
	public void onBackPressed() { 
		Profil.this.finish();
	}
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
    }
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        setContentView(R.layout.profil);
        
        
        fungsi_menu();
        
		nameValuePairs = new ArrayList();
		
		imgLoader = new ImageLoader(getApplicationContext());
		
		session = new SessionManager(getApplicationContext());
	    
	    HashMap<String, String> user = session.getUserDetails();
	     
	    // name
	    session_userid = user.get(SessionManager.id_users);
	    session_username = user.get(SessionManager.username);
	    session_password = user.get(SessionManager.password);
	    session_email = user.get(SessionManager.email);
	    session_city = user.get(SessionManager.city);
	    session_checked = user.get(SessionManager.checked);
		
	 // getting intent data
        Intent in = getIntent();
         
        // Get XML values from previous intent
        id_ = session_userid;
        //String title_ = in.getStringExtra(KEY_MERCHANT_NAME);
        //String artist_ = in.getStringExtra(KEY_VOUCHER_NAME);
        //String duration_ = in.getStringExtra(KEY_VOUCHER_TOTAL);
        //String thumb_url_ = in.getStringExtra(KEY_MERCHANT_PHOTO);
        
        new loadListView().execute();
             
    }
	
	
	public void fungsi_menu(){
		
		setSlidingActionBarEnabled(true);
		 
        sm = getSlidingMenu();
		 
		LinearLayout header_vertical_divider_1 = (LinearLayout) findViewById(R.id.header_vertical_divider_1);
		LinearLayout header_vertical_divider_2 = (LinearLayout) findViewById(R.id.header_vertical_divider_2);
		ImageView diskonion_iconmenu = (ImageView) findViewById(R.id.diskonion_iconmenu);
		ImageView diskonion_iconhome = (ImageView) findViewById(R.id.diskonion_iconhome);
		ImageView diskonion_iconsearch = (ImageView) findViewById(R.id.diskonion_iconsearch);
		ImageView diskonion_iconcategory = (ImageView) findViewById(R.id.diskonion_iconcategory);
		
		 
		
		diskonion_iconmenu.setOnClickListener(new OnClickListener() { 
	 			public void onClick(View v)  {
				sm.showMenu(); 
			}
		}); 
		
		diskonion_iconhome.setOnClickListener(new OnClickListener() { 
	 			public void onClick(View v)  {
				//sm.showSecondaryMenu();
 				Intent intent=new Intent(getApplicationContext(),Home.class);
 				startActivity(intent);
			}
		});
		
		diskonion_iconsearch.setOnClickListener(new OnClickListener() { 
	 			public void onClick(View v)  {
				//sm.showSecondaryMenu();
 				Intent intent=new Intent(getApplicationContext(),Search.class);
 				startActivity(intent);
			}
		});
		
		diskonion_iconcategory.setOnClickListener(new OnClickListener() { 
	 			public void onClick(View v)  {
				sm.showSecondaryMenu(); 
			}
		});
 		
	}
	
	
	@SuppressLint("NewApi")
	private class loadListView extends AsyncTask<Void, Void, Void>  {

		@Override
		protected void onPreExecute() {
			
			 
			// Showing progress dialog before sending http request
			pDialog = new ProgressDialog(
					Profil.this);
			pDialog.setMessage("Retrieve Data, Please wait...");
			pDialog.setIndeterminate(true);
			pDialog.setCancelable(false);
			pDialog.show();
		}

		protected Void doInBackground(Void... unused) {
			code="99";
			msg="Not connected to server";
			
				Global.URL = Global.link_profil + id_;
	    		//System.out.println(Global.URL);
	    		//System.out.println(id_);
	    		////System.out.println(title_);
	    		//http post
				
				
				try{ 
					json=JSONfunctions.getStringContent(Global.URL);
					jsonobject=new JSONObject(json); 
					code=jsonobject.getString("code");
					msg=jsonobject.getString("msg");
					 
				}
				catch (Exception e) 
				{
					e.printStackTrace();
					System.out.println("catch code msg");
					System.out.println(e);
					//pDialog.dismiss();
					//Toast.makeText(getApplicationContext(), "Something wrong in your connection", Toast.LENGTH_LONG).show();
				
				}
				
				System.out.println("code "+code);
				if(code.equalsIgnoreCase("99")){
					try{ 
						System.out.println(Global.URL);
						//json=JSONfunctions.getStringContent2(Global.URL);
						System.out.println(json); 
						 
					}
					catch (Exception e) 
					{
						e.printStackTrace();
						System.out.println("catch 1");
						System.out.println(e);

						//pDialog.dismiss();
						//Toast.makeText(getApplicationContext(), "Something wrong in your connection (1)", Toast.LENGTH_LONG).show();
					
					}
				}
				
				  
			return null;
		}
		
		
		protected void onPostExecute(Void unused) {
			result = json;
			if(code.equalsIgnoreCase("0")){
				
				Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
			}
			else{
				try{
			
			      	JSONArray jArray = new JSONArray(result);
			      	JSONObject json_data=null;
	
			      	//System.out.println("array length");
			      	//System.out.println(jArray.length());
			      	
			      	for(int i=0;i<jArray.length();i++){
							json_data = jArray.getJSONObject(i); 
							
							   
							
							xml_users_id = json_data.getString(KEY_USERS_ID);
							xml_users_username = json_data.getString(KEY_USERS_USERNAME);
							xml_users_email = json_data.getString(KEY_USERS_EMAIL);
							xml_users_city = json_data.getString(KEY_USERS_CITY);
							xml_users_photo = json_data.getString(KEY_USERS_PHOTO);
							xml_users_area = "(+" + json_data.getString(KEY_USERS_AREA) + ") "; 
							xml_users_phone = json_data.getString(KEY_USERS_PHONE); 
	
							xml_users_photo = Global.path_photo + xml_users_photo; 
							 
							
							//System.out.println(xml_users_id);
							//System.out.println(xml_users_username);
							//System.out.println(xml_users_email);
							//System.out.println(xml_users_city);
							//System.out.println(xml_users_photo); 
							//System.out.println(xml_users_area); 
							//System.out.println(xml_users_phone); 
						 	 
			      		}
			      	
				}catch(JSONException e1){ 
					System.out.println("catch 2");
					System.out.println(e1);
					Toast.makeText(getApplicationContext(), "Something wrong in your connection (2)", Toast.LENGTH_LONG).show();
				}catch (ParseException e1){ 
					System.out.println("catch 3");
					System.out.println(e1);
					Toast.makeText(getApplicationContext(), "Something wrong in your connection (3)", Toast.LENGTH_LONG).show();	
				} 
			}
			TextView txt_users_id = (TextView)findViewById(R.id.txt_users_id);
			TextView txt_users_username = (TextView)findViewById(R.id.txt_users_username); 
	        //TextView txt_users_email = (TextView)findViewById(R.id.txt_users_email); // title
	        TextView txt_users_phone = (TextView)findViewById(R.id.txt_users_phone); // title
	        TextView txt_users_city = (TextView)findViewById(R.id.txt_users_city); // title
	        ImageView txt_users_image =(ImageView)findViewById(R.id.txt_users_image); // thumb image
	        
	        //txt_channel_id.setVisibility(View.GONE);
		        
	        txt_users_id.setText(xml_users_id);
	        txt_users_username.setText(xml_users_username);
	        //txt_users_email.setText(xml_users_email);
	        txt_users_phone.setText(xml_users_area + xml_users_phone);
	        txt_users_city.setText(xml_users_city);
		    imgLoader.DisplayImage(xml_users_photo, txt_users_image);
		        
		        
			pDialog.dismiss();
		}
	}
	
	  
	
	
}
